const vm = new Vue({
  el: "#app",
  data: {
    username: '',
    loginBtn: true,
    welcome: false,
    topicList: []
  },
  methods: {
    getData(){
      this.$http.get('/getTopic').then(res => {
        this.topicList = res.body
        
        this.$http.get("/getUser").then( res => {
          this.username = res.body.username
  
          if (this.username) {  //如果已登录 改变一些元素的显示隐藏
            this.loginBtn = ! this.loginBtn
            this.welcome = ! this.welcome
  
            //找出当前登录用户发表的留言，设置删除按钮可见
            this.topicList.some(element => {
              if (element.username == this.username) {
                element.delBtn = !element.delBtn
              }
            });
          }
        })
      })

      
    },
    delTopic(id){   //通过id发起删除请求
      this.$http.post('/del', { id: id }).then(res => {
        if (res.body == false) {
          alert('系统繁忙')
        } else {
          alert('删除成功')
          window.location.href = "/"
        }
      })
    }
  },
  mounted() {
    this.getData()
  },
})